//
//  YKPullDownViewController.m
//  下拉放大图片效果
//
//  Created by 叶文凯 on 16/11/25.
//  Copyright © 2016年 葱花思鸡蛋. All rights reserved.
//

#import "YKPullDownViewController.h"
#import "UINavigationController+YKGestureReplace.h"

static NSInteger headerHeight = 200;
@interface YKPullDownViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation YKPullDownViewController{
    UIView *_headerView;
    UIImageView *_imgView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   
    [self setupUI];
}
//搭建界面
- (void)setupUI
{
    [self prepareTableView];
    [self prepareHeaderView];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    //取消自适应nagivationBar的高度
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

//准备tableview
- (void)prepareTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    //设置tableView的偏移量
    tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, 0, 0);
    
}
//准备头部视图
- (void)prepareHeaderView
{
    CGRect frame = CGRectMake(0, 0, self.view.bounds.size.width, headerHeight);
    _headerView = [[UIView alloc] initWithFrame:frame];
    _headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_headerView];
    
    _imgView = [[UIImageView alloc] initWithFrame:_headerView.bounds];
    _imgView.image = [UIImage imageNamed:@"001.jpg"];
    //设置图片的填充模式
    _imgView.contentMode = UIViewContentModeScaleAspectFill;
    // 切掉多余部分
    _imgView.clipsToBounds = YES;
    [_headerView addSubview:_imgView];
    
}


#pragma mark - 实现数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    return cell;
}
#pragma mark - 实现代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offset = scrollView.contentOffset.y + scrollView.contentInset.top;
//    NSLog(@"%lf",offset);
    if (offset <= 0) {
        
        self.navigationController.navigationBar.hidden = YES;
        _headerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, headerHeight - offset);
        _imgView.alpha = 1.0;
        NSLog(@"向下拖拽透明度：%lf",_imgView.alpha);
        NSLog(@"向下拖拽");
    }
    else
    {
        //设置headerView的Y轴偏移量的最小值
        offset = MIN(offset, headerHeight - 64);
        _headerView.frame = CGRectMake(0, -offset, self.view.bounds.size.width, headerHeight);
//        NSLog(@"-----%lf",scrollView.contentOffset.y);
        self.navigationController.navigationBar.hidden = NO;
        self.navigationController.navigationBar.alpha = MIN(offset/(headerHeight - 64), 1);
        _imgView.alpha =MAX(1 - self.navigationController.navigationBar.alpha, 0) ;
        NSLog(@"向上拖拽");
        NSLog(@"向上拖拽透明度：%lf",_imgView.alpha);
    }
    _imgView.frame = _headerView.bounds;
  
}


@end
